﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;

namespace Library
{
    public partial class BookTypeBook : Form
    {
        LibraryEntities1 ins = new LibraryEntities1();
        public static ObjectSet<BookType> bookTypes;
        public BookTypeBook()
        {
            InitializeComponent();
            bookTypes = ins.BookType;
        }

        private void выходToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BookTypeBook_Load(object sender, EventArgs e)
        {
            bookView.Columns.Add("idType", "idType");
            bookView.Columns["idType"].Visible = false;

            bookView.Columns.Add("Type", "Тип");
            bookView.Columns["Type"].Width = 300;
            bookView.Columns["Type"].ReadOnly = true;


            int i = 0;
            foreach (BookType x in bookTypes)
            {
                bookView.Rows.Add();
                bookView.Rows[i].Cells[0].Value = x.idBookType;
                bookView.Rows[i].Cells[1].Value = x.TypeName;
                i++;
            }
        }

        private void редактироватьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ins = new LibraryEntities1();
            bookTypes = ins.BookType;

            if (bookView.CurrentRow != null)
            {

                int idType = Convert.ToInt32(bookView.CurrentRow.Cells[0].Value);
                BookType type = bookTypes.Where(x => x.idBookType==idType).FirstOrDefault();
                BookTypeViewForm fm = new BookTypeViewForm(type, false);
                fm.ShowDialog();
                bookView.CurrentRow.Cells[1].Value = fm.bookType.TypeName;
            }
            else
            {
                MessageBox.Show("Выберите запись для редактирования");
            }
        }

        private void добавитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BookTypeViewForm fm = new BookTypeViewForm(null, true);
            fm.ShowDialog();
            if (fm.bookType != null)
            {
                bookView.Rows.Add();

                bookView.Rows[bookView.Rows.Count - 1].Cells[0].Value = fm.bookType.idBookType;
                bookView.Rows[bookView.Rows.Count - 1].Cells[1].Value = fm.bookType.TypeName;
            }
        }

        private void удалитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (bookView.CurrentRow != null)
            {

                int elementId = Convert.ToInt32(bookView.CurrentRow.Cells[0].Value);
                if (bookTypes.Where(x => x.idBookType==elementId).Count() == 1)
                {
                    BookType element = bookTypes.Where(x => x.idBookType==elementId).FirstOrDefault();
                    if (element.Book.Count > 0)
                        MessageBox.Show("Невозможно удаление. Есть книги такого типа");
                    else
                    {
                        bookTypes.DeleteObject(element);
                        ins.SaveChanges();
                        bookView.Rows.Remove(bookView.CurrentRow);
                    }
                }
                else
                    bookView.Rows.Remove(bookView.CurrentRow);

            }
            else
            {
                MessageBox.Show("Выберите строку для удаления");
            }
        }
    }
}
